package com.gr3g.dev.learndraw.event;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.gr3g.dev.learndraw.DrawActivity;
import com.gr3g.dev.learndraw.FreeDrawActivity;

/** @author Gregory Tardivel */
public final class DrawHandler extends Handler {

    /** Fenetre d'attente. */
    private ProgressDialog wait;
    /** Dessin valide. */
    private String valid;
    /** Activity parent. */
    private final Activity parent;

    public static final int MSG_ERR = 0;
    public static final int MSG_CNF = 1;
    public static final int MSG_IND = 2;

    /** Constructeur.
     * @param pParent
     * @param pRender */
    public DrawHandler(final Activity pParent) {
        super();
        this.parent = pParent;
    }

    /** @param pWait the wait to set */
    public void setWait(final ProgressDialog pWait) {
        this.wait = pWait;
    }

    /** @param pValid the valid to set */
    public void setValid(final String pValid) {
        this.valid = pValid;
    }

    @Override
    public void handleMessage(final Message msg) {
        String text2display = null;
        switch (msg.what) {
            case MSG_IND:
                if (this.wait.isShowing()) {
                    this.wait.setMessage((String) msg.obj);
                }
                break;
            case MSG_ERR:
                text2display = (String) msg.obj;
                Toast.makeText(this.parent, "Error: " + text2display,
                        Toast.LENGTH_LONG).show();
                if (this.wait.isShowing()) {
                    this.wait.dismiss();
                }
                break;
            case MSG_CNF:
                text2display = (String) msg.obj;
                Toast.makeText(this.parent, "Info: " + text2display,
                        Toast.LENGTH_SHORT).show();
                if (this.wait.isShowing()) {
                    this.wait.dismiss();
                }
                if (this.parent instanceof DrawActivity) {
                    final DrawActivity activity = (DrawActivity) this.parent;
                    if (!this.valid.equals("OK")) {
                        new AlertDialog.Builder(this.parent).setMessage(
                                this.valid).setPositiveButton("OK",
                                activity.resetEtape).create().show();
                    } else {
                        activity.nextEtape();
                    }
                } else if (this.parent instanceof FreeDrawActivity) {
                    final FreeDrawActivity activity = (FreeDrawActivity) this.parent;
                    if (!this.valid.equals("OK")) {
                        new AlertDialog.Builder(this.parent).setMessage(
                                this.valid).setPositiveButton("OK", null)
                                .create().show();
                    } else {
                        activity.saveFinish();
                    }
                }
                break;
            default:
                break;
        }
    }
}
